package com.example.groupservice.controller;


import com.example.groupservice.service.GroupInfoService;
import org.example.annotation.GlobalInteceptor;
import org.example.controller.ABaseController;
import org.example.entity.dto.TokenUserInfoDto;
import org.example.entity.enums.ResponseCodeEnum;
import org.example.entity.enums.UserContactStatusEnum;
import org.example.entity.po.GroupInfo;
import org.example.entity.query.UserContactQuery;
import org.example.entity.vo.GroupInfoVO;
import org.example.entity.vo.ResponseVO;
import org.example.exception.BusinessException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * @ClassName GroupController
 * @Description
 * @Author 程序员老罗 https://space.bilibili.com/499388891
 * @Date 2023/12/24 20:04
 */
@RestController("groupController")
@RequestMapping("/group")
public class GroupController extends ABaseController {
    @Resource
    private GroupInfoService groupInfoService;


    @RequestMapping("/saveGroup")
    @GlobalInteceptor(checkLogin = true)
    public ResponseVO saveGroup(HttpServletRequest request,
                                String groupId,
                                @NotEmpty String groupName,
                                String groupNotice,
                                @NotNull Integer joinType,
                                MultipartFile avatarFile,
                                MultipartFile avatarCover){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        groupInfoService.saveGroup(tokenUserInfo,groupId,groupName,groupNotice,joinType,avatarCover,avatarFile);
        return getSuccessResponseVO(null);
    }

    @RequestMapping("/loadMyGroup")
    @GlobalInteceptor(checkLogin = true)
    public ResponseVO loadMyGroup(HttpServletRequest request){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        List<GroupInfo> groupInfos = groupInfoService.loadMyGroup(tokenUserInfo);
        return getSuccessResponseVO(groupInfos);
    }

    @RequestMapping("/getGroupInfo")
    @GlobalInteceptor(checkLogin = true)
    public ResponseVO getGroupInfo(HttpServletRequest request,String groupId){

        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        GroupInfo groupInfo = groupInfoService.getGroupInfo(tokenUserInfo, groupId);

        return getSuccessResponseVO(groupInfo);
    }


    @RequestMapping("/getGroupInfo4Chat")
    @GlobalInteceptor(checkLogin = true)
    public ResponseVO getGroupInfo4Chat(HttpServletRequest request,String groupId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        GroupInfo groupInfo = groupInfoService.getGroupInfo(tokenUserInfo,groupId);

        UserContactQuery query = new UserContactQuery();
        query.setContactId(groupId);
        query.setQueryUserInfo(true);
        query.setOrderBy("create_time desc");
        query.setStatus(UserContactStatusEnum.FRIEND.getStatus());
        //TODO 待完成 null改
//        List<UserContact> listByParam = userContactService.findListByParam(query);

        GroupInfoVO groupInfo1 = new GroupInfoVO();
        groupInfo1.setGroupInfo(groupInfo);
        groupInfo1.setUserContactList(null);

        return getSuccessResponseVO(groupInfo1);
    }

    @RequestMapping("/leaveGroup")
    @GlobalInteceptor
    public ResponseVO leaveGroup(HttpServletRequest request,@NotNull String groupId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        groupInfoService.leaveGroup(tokenUserInfo,groupId);

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/dissolutionGroup")
    @GlobalInteceptor
    public ResponseVO dissolutionGroup(HttpServletRequest request,@NotNull String groupId){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        GroupInfo info = groupInfoService.getGroupInfoByGroupId(groupId);
        if (info == null || !info.getGroupOwnerId().equals(tokenUserInfo.getUserId())){
            throw new BusinessException(ResponseCodeEnum.CODE_600);
        }
        groupInfoService.dissolutionGroup(tokenUserInfo.getUserId(),groupId);

        return getSuccessResponseVO(null);
    }

    @RequestMapping("/addOrRemoveGroupUser")
    @GlobalInteceptor
    public ResponseVO addOrRemoveGroupUser(HttpServletRequest request,@NotNull String groupId,String selectContacts,Integer opType){
        TokenUserInfoDto tokenUserInfo = getTokenUserInfo(request);
        groupInfoService.addOrRemoveGroupUser(tokenUserInfo.getUserId(),groupId,selectContacts,opType);

        return getSuccessResponseVO(null);
    }
}
